package com.youlu.campus.service.jinshuju;

import com.mongodb.bulk.BulkWriteResult;
import com.youlu.campus.entity.jinshuju.JinMockSignUserImport;
import com.youlu.campus.entity.jinshuju.JinUserInviteImport;
import com.youlu.campus.entity.jinshuju.dto.QueryJinUserImportDTO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.BulkOperations;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.repository.support.PageableExecutionUtils;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

@Slf4j
@Service
public class JinUserImportService {

    @Autowired
    private MongoTemplate mongoTemplate;

    public Page<JinMockSignUserImport> feedList(QueryJinUserImportDTO dto){
        String activityId = dto.getActivityId();
        Query query = new Query();
        query.addCriteria(Criteria.where("activityId").is(activityId));
        long count = this.mongoTemplate.count(query, JinMockSignUserImport.class);
        PageRequest pageRequest = PageRequest.of(dto.getPageNo() - 1, dto.getPageSize());
//        query.with(new Sort(Sort.Direction.DESC, "id"));
        Sort sort=Sort.by(Sort.Direction.DESC, "id");
        query.with(sort);
        List<JinMockSignUserImport> userImportList = this.mongoTemplate.find(query.with(pageRequest),
                JinMockSignUserImport.class);
        return PageableExecutionUtils.getPage(userImportList, pageRequest, () -> count);
    }

    public void batchDelete(String activityId,Class clz){
        Query query = new Query();
        query.addCriteria(Criteria.where("activityId").is(activityId));
        BulkOperations bulkOperations = this.mongoTemplate.bulkOps(BulkOperations.BulkMode.UNORDERED,
                clz);
        bulkOperations.remove(query);
        BulkWriteResult execute = bulkOperations.execute();
        int deletedCount = execute.getDeletedCount();
        log.info("批量删除活动:{} 数据:{}",activityId,deletedCount);
    }

    public void delete(String id){
        Query query = new Query();
        query.addCriteria(Criteria.where("id").is(id));
        this.mongoTemplate.remove(query, JinUserInviteImport.class);
    }

    public JinUserInviteImport detail(String id){
        return this.mongoTemplate.findById(id, JinUserInviteImport.class);
    }

    public Page<JinUserInviteImport> list(QueryJinUserImportDTO dto){
        String activityId = dto.getActivityId();
        Query query = new Query();
        query.addCriteria(Criteria.where("activityId").is(activityId));
        long count = this.mongoTemplate.count(query, JinUserInviteImport.class);
        PageRequest pageRequest = PageRequest.of(dto.getPageNo() - 1, dto.getPageSize());
//        query.with(new Sort(Sort.Direction.DESC, "id"));
        Sort sort=Sort.by(Sort.Direction.DESC, "id");
        query.with(sort);
        List<JinUserInviteImport> userImportList = this.mongoTemplate.find(query.with(pageRequest), JinUserInviteImport.class);
        return PageableExecutionUtils.getPage(userImportList, pageRequest, () -> count);
    }

    public void userImport(String activityId, List<JinUserInviteImport> insertUserImport ){
        log.info("导入 活动:{} 用户数据:{}",activityId,insertUserImport.size());
        batchDelete(activityId,JinUserInviteImport.class);
        Date now = new Date();
        List<JinUserInviteImport> batchList =
                insertUserImport.stream().peek(jinUserInviteImport ->{
                    jinUserInviteImport.setCreatedTime(now);
                    jinUserInviteImport.setActivityId(activityId);
                }).collect(Collectors.toList());
        mongoTemplate.insertAll(batchList);
    }

    public void userFeedImport(String activityId, List<JinMockSignUserImport> insertUserImport ){
        log.info("导入 活动:{} 用户数据:{}",activityId,insertUserImport.size());
        batchDelete(activityId,JinMockSignUserImport.class);
        Date now = new Date();
        List<JinMockSignUserImport> batchList =
                insertUserImport.stream().peek(jinUserInviteImport ->{
                    jinUserInviteImport.setCreatedTime(now);
                    jinUserInviteImport.setActivityId(activityId);
                }).collect(Collectors.toList());
        mongoTemplate.insertAll(batchList);
    }
}
